//
//  KeChengNextNavView.m
//  SanYiTeaching
//
//  Created by 李文强 on 2019/4/17.
//  Copyright © 2019 SanYi. All rights reserved.
//

#import "KeChengNextNavView.h"

@implementation KeChengNextNavView

- (instancetype)initWithFrame:(CGRect)frame{
    if ([super initWithFrame:frame]) {
        [self initSubView];
    }
    return self;
}
- (void)initSubView{
    self.backgroundColor = MAINBACKCOLOR;
    [self addSubview:self.weiJieYeButton];
    [self addSubview:self.jieYeButton];
    [self addSubview:self.backButton];
    [_backButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@10);
        make.top.equalTo(@10);
        make.height.equalTo(@24);
        make.width.equalTo(@60);
    }];
    [_weiJieYeButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.mas_centerX).offset(-50);
        make.centerY.equalTo(self.mas_centerY);
        make.width.equalTo(@90);
        make.height.equalTo(@15);
    }];
    [_jieYeButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.mas_centerX).offset(50);
        make.centerY.equalTo(self.mas_centerY);
        make.width.equalTo(@90);
        make.height.equalTo(@15);
    }];
    
    _weiJieYeButton.selected = YES;
    _lineView = [[UIView alloc]init];
    _lineView.backgroundColor = [UIColorChange colorwithHexString:@"#20B097"];
    _lineView.frame = CGRectMake(0, 0, 80, 2);
    _lineView.center = CGPointMake(_weiJieYeButton.center.x, _weiJieYeButton.center.y+15);
    [self addSubview:self.lineView];
    [_lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.mas_centerX).offset(-50);
        make.bottom.equalTo(self.mas_bottom);
        make.width.equalTo(@90);
        make.height.equalTo(@2);
    }];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(changeView:) name:@"ChangeCellSpaNotice" object:nil];
}
/*
 这里没有把两个按钮作为移动标准，从新计算了左边
 之前以按钮为基准量约束后出现按钮跟线移动，原因不明
 目前以父类的View为约束的标准
 */
- (void)changeView:(NSNotification *)info{
    BOOL type = [[info.userInfo objectForKey:@"Type"] intValue];
    self.Type = type;
    if (type == YES) {
        //View变大
        [_weiJieYeButton mas_updateConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(self.mas_centerX).offset(-100);
            make.centerY.equalTo(self.mas_centerY);
            make.width.equalTo(@90);
            make.height.equalTo(@15);
        }];
        [_jieYeButton mas_updateConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.mas_centerX).offset(100);
            make.centerY.equalTo(self.mas_centerY);
            make.width.equalTo(@90);
            make.height.equalTo(@15);
        }];
        //矫正l线条位置
        if (_weiJieYeButton.selected == YES) {
            [_lineView mas_updateConstraints:^(MASConstraintMaker *make) {
                make.right.equalTo(self.mas_centerX).offset(-100);
            }];
        }else{
            [_lineView mas_updateConstraints:^(MASConstraintMaker *make) {
                make.right.equalTo(self.mas_centerX).offset(100+90);
            }];
        }
    }else{
        //View变小
        [_weiJieYeButton mas_updateConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(self.mas_centerX).offset(-50);
            make.centerY.equalTo(self.mas_centerY);
            make.width.equalTo(@90);
            make.height.equalTo(@15);
        }];
        [_jieYeButton mas_updateConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.mas_centerX).offset(50);
            make.centerY.equalTo(self.mas_centerY);
            make.width.equalTo(@90);
            make.height.equalTo(@15);
        }];
        //矫正l线条位置
        if (_weiJieYeButton.selected == YES) {
            [_lineView mas_updateConstraints:^(MASConstraintMaker *make) {
                make.right.equalTo(self.mas_centerX).offset(-50);
            }];
        }else{
            [_lineView mas_updateConstraints:^(MASConstraintMaker *make) {
                make.right.equalTo(self.mas_centerX).offset(50+90);
            }];
        }
    }
    
}

- (UIButton *)weiJieYeButton{
    if (!_weiJieYeButton) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        [button setTitle:@"课程学生" forState:UIControlStateNormal];
        [button setTitleColor:[UIColor colorWithHexString:@"#20B097"] forState:UIControlStateSelected];
        [button setTitleColor:TITLECOLOR forState:UIControlStateNormal];
        button.titleLabel.font = [UIFont systemFontOfSize:16];
        [button addTarget:self action:@selector(noClass:) forControlEvents:UIControlEventTouchUpInside];
        _weiJieYeButton = button;
    }
    return _weiJieYeButton;
}
- (void)noClass:(UIButton *)sender{
    _jieYeButton.selected = NO;
    sender.selected = YES;
    if (self.Type == YES) {
        [_lineView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(self.mas_centerX).offset(-100);
        }];
    }else{
        [_lineView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(self.mas_centerX).offset(-50);
        }];
    }
    [self.delegate leftButtonClick];
    //未上课时通知
    //    [[NSNotificationCenter defaultCenter]postNotificationName:@"WeiShangKeShiNotice" object:nil];
}
- (UIButton *)jieYeButton{
    if (!_jieYeButton) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        [button setTitle:@"课程资源" forState:UIControlStateNormal];
        [button setTitleColor:[UIColor colorWithHexString:@"#20B097"] forState:UIControlStateSelected];
        [button setTitleColor:TITLECOLOR forState:UIControlStateNormal];
        [button addTarget:self action:@selector(haveClass:) forControlEvents:UIControlEventTouchUpInside];
        button.titleLabel.font = [UIFont systemFontOfSize:16];
        _jieYeButton = button;
    }
    return _jieYeButton;
}

- (void)selectKeShiZiYuan {
    [self haveClass:_jieYeButton];
}

- (void)haveClass:(UIButton *)sender{
    _weiJieYeButton.selected = NO;
    sender.selected = YES;
    if (self.Type == YES) {
        [_lineView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(self.mas_centerX).offset(190);
        }];
    }else{
        [_lineView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(self.mas_centerX).offset(50+90);
        }];
    }
    [self.delegate rightButtonClick];
    //一上课时通知
    //    [[NSNotificationCenter defaultCenter]postNotificationName:@"YiShangKeShiNotice" object:nil];
}
- (UIButton *)backButton{
    if (!_backButton) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        [button setTitle:@" 返回" forState:UIControlStateNormal];
        [button setTitleColor:[UIColorChange colorwithHexString:@"SELECTEDCOLOR"] forState:UIControlStateNormal];
        [button setImage:[UIImage imageNamed:@"back_color"] forState:UIControlStateNormal];
        button.titleLabel.font = [UIFont systemFontOfSize:12];
        [button setTitleColor:SELECTEDCOLOR forState:0];
        [button addTarget:self action:@selector(back:) forControlEvents:UIControlEventTouchUpInside];
        _backButton = button;
    }
    return _backButton;
}
- (void)back:(UIButton *)sender{
    [self.delegate backClick];
}
@end
